package com.igroupes.ovineherd.model.Style;

import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class DateTimeRangeStyle extends DateTimeStyle {
    public DateTimeRangeStyle(String label, Object value, Boolean required, String minDate, String maxDate) {
        super(label, value, required);
        this.type = FormKey.DATETIMERANGE.getCode();
    }
    @Override
    public Boolean verify(Object o) {
        if(!(o instanceof DateTimeRangeStyle)){
            throw new ModelException("校验style类类型错误，dateRange");
        }
        DateTimeRangeStyle style = (DateTimeRangeStyle) o;
        if(!style.getDisabled()){
            super.verify(o);
            if (!(style.getRequired() instanceof Boolean)){
                throw new ModelException("required格式错误");
            }
            List<String> value = (List<String>) style.getValue();
            if(style.getRequired() ){
                if(StringUtils.isBlank(value.get(0))){
                    throw new ModelException(style.getLabel()+"值是必传字段");
                }
                if(StringUtils.isBlank(value.get(1))){
                    throw new ModelException(style.getLabel()+"值是必传字段");
                }
            }
            if(StringUtils.isNotBlank(value.get(0))){
                if(!(style.getValue() instanceof String)){
                    throw new ModelException(style.getLabel()+"值必须是字符串");
                }

                if(!DateUtils.isValidDate(value.get(0), DateUtils.DATETIME_FORMAT)){
                    throw new ModelException(style.getLabel()+"时间格式不正确,"+DateUtils.DATETIME_FORMAT);
                }
            }
            if(StringUtils.isNotBlank(value.get(1))){
                if(!(style.getValue() instanceof String)){
                    throw new ModelException(style.getLabel()+"值必须是字符串");
                }

                if(!DateUtils.isValidDate(value.get(1), DateUtils.DATETIME_FORMAT)){
                    throw new ModelException(style.getLabel()+"时间格式不正确,"+DateUtils.DATETIME_FORMAT);
                }
            }
        }
        return true;
    }
}
